#include"StdAfx.h"

pgPixelShader::pgPixelShader()
{
	m_pPixelShader = NULL;
}

pgPixelShader::~pgPixelShader()
{
	m_pPixelShader = NULL;
}

bool pgPixelShader::CompileFromFile(LPCWSTR lpFilePath)
{
	
	PG_LOG_INFO_EX(TOPIC_PGRENDERER, "Compiling pixel shader from file: %s!", pgString::LPCWSTRToStdString(lpFilePath).c_str());

	ID3DBlob *pPSBlob = NULL;
	 HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

    ID3DBlob* pErrorBlob;
    hr = D3DX11CompileFromFile( lpFilePath, NULL, NULL, "PS", "ps_4_0", 
        dwShaderFlags, 0, NULL, &pPSBlob, &pErrorBlob, NULL );
    if( FAILED(hr) )
    {
        if( pErrorBlob != NULL )
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
        if( pErrorBlob ) pErrorBlob->Release();

		PG_DX_LOG_ERROR(hr, __FILE__, __LINE__);
        return false;
    }
    if( pErrorBlob ) pErrorBlob->Release();
	
	hr = g_PG_pRenderer->m_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &m_pPixelShader );
	pPSBlob->Release();
    if( FAILED( hr ) )
	{
		PG_DX_LOG_ERROR(hr, __FILE__, __LINE__);
        return false;
	}

	return true;
}

void pgPixelShader::ReleaseShader()
{
	PG_SAFE_RELEASE(m_pPixelShader);
}